<?php

namespace app\controller;

use app\BaseController;
use app\model\Goods as GoodsModel;
use app\Request;
use app\validate\Goods as GoodsValidate;
use think\Db;
use think\facade\Filesystem;

class Goods extends BaseController
{

    public function index(Request $request)
    {
        try {
            $param = $request->param();
            $model = new GoodsModel();
            $list = $model->paginate(10);
            return show(1, $list);

        } catch (\Exception $e) {
            return show(0, $e->getMessage());
        }

    }


    public function add(Request $request)
    {
        $param = $request->param();
        $model = new GoodsModel();
        $model->startTrans();
        try {
            validate(GoodsValidate::class)->scene('add')->check($param);
            $data = $model->create($param);
            $model = $this->addSpace($param, $data, $model);
            $model->commit();
            return show(1, '新增商品成功');
        } catch (\Exception $e) {
            return show(0, $e->getMessage());
        }
    }

    //添加进规格表
    public function addSpace($param, $data, $model)
    {
        $spaceArr = [];
        foreach ($param['spaceArr'] as $s) {
            $spaceArr[] = [

                'space_values' => $s['space_values'],
                'goods_id' => $data['id'],
                'space_name'=>$s['space_name'],
                'stock'=>$s['stock'],
                'space_price'=>$s['space_price']

            ];
        }

       // return show(1,$spaceValues);
        $model->space()->saveAll($spaceArr);

        return $model;
    }

    public function edit(Request $request)
    {
        $param = $request->param();
        $model = new GoodsModel();
        try {

        } catch (\Exception $e) {
            show(0, $e->getMessage());
        }
    }

    public function upload()
    {


        $file = \think\facade\Request::file('file');

        if ($file) {
            try {
                // 将文件保存为临时文件
                $savename = Filesystem::putFile('temp', $file);
// 获取临时文件的完整路径
                $tempFilePath = Filesystem::path($savename);
                // 获取临时文件的扩展名
                $extension = pathinfo($tempFilePath, PATHINFO_EXTENSION);
                // 生成新的文件名
                $newFileName = uniqid() . '.' . $extension;
                // 确定新的图片文件的保存目录
                $savePath = Filesystem::disk('public')->path('uploads/' . $newFileName);
                // 将临时文件移动到指定目录，并重命名为新的文件名
                rename($tempFilePath, $savePath);
// 返回新的图片文件路径
                $newFilePath = '/storage/uploads/' . $newFileName;
                return show(1, $newFilePath);
//                $info = $file->move(config('app.upload_path'));
//                if ($info) {
//                    $url = $info->getFilename();
//                    $pub = public_path();
//                    //文件上传成功
//                    return show(1,$pub);
//                } else {
//                    return show(0, '上传失败');
//                }
            } catch (\Exception $e) {
                return show(0, $e->getMessage());
            }

        } else {
            return show(0, '未选择文件');
        }
    }
}